package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.entity.DutyTemplate;
import com.core.website.entity.DutyTemplateDetail;
import com.core.website.entity.User;
import com.core.website.mapper.RoleMapper;
import com.core.website.service.DutyService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/dutyApi")
public class DutyApi {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private DutyService dutyService;
    @PostMapping("/addTemplate")
    public Result addTemplate(@RequestBody DutyTemplate dutyTemplate){
        int result=dutyService.addTemplate(dutyTemplate);
        return ResultUtil.success(result);
    }
    @GetMapping("/queryTemplateAll")
    public Result queryTemplateAll(){
        List<DutyTemplate> result=dutyService.queryTemplateAll();
        return ResultUtil.success(result);
    }
    @GetMapping("/queryTemplateOne")
    public Result queryOne(String id){
        DutyTemplate result=dutyService.queryTemplateOne(id);
        return ResultUtil.success(result);
    }
    @PostMapping("/addTemplateDetail")
    public Result addTemplateDetail(@RequestBody DutyTemplateDetail dutyTemplateDetail){
        int result=dutyService.addTemplateDetail(dutyTemplateDetail);
        return ResultUtil.success(result);
    }
    @PostMapping("/delTemplateDetail")
    public Result delTemplateDetail(@RequestBody List<String> ids){
        int result=dutyService.delTemplateDetail(ids);
        return ResultUtil.success(result);
    }
    @GetMapping("/queryDetailByTemplateId")
    public Result queryDetailByTemplateId(String templateId){
        List<DutyTemplateDetail> result=dutyService.queryDetailByTemplateId(templateId);
        return ResultUtil.success(result);
    }
    @GetMapping("/queryDutyByDay")
    public Result queryDutyByDay(String day){
        List<Map<String,Object>> result=dutyService.queryDutyByDay(day);
        return ResultUtil.success(result);
    }
    @GetMapping("/queryTemplateByDay")
    public Result queryTemplateByDay(String day){
        DutyTemplate result=dutyService.queryTemplateByDay(day);
        return ResultUtil.success(result);
    }
    @PostMapping("/addDuty")
    public Result addDuty(@RequestBody Map<String,Object> param){
        String day = param.get("day").toString();
        String templateId = param.get("templateId").toString();
        List<Map<String,Object>> dutyList=(List<Map<String,Object>>)param.get("dutyList");
        int result= 0;
        try {
            result = dutyService.addDuty(dutyList,day,templateId);
        } catch (ParseException e) {
            return ResultUtil.error(-1,"时间转换失败，需要yyyy-MM-dd类型时间");
        }
        return ResultUtil.success(result);
    }
    @PostMapping("/updateDuty")
    public Result updateDuty(@RequestBody Map<String,Object> param){
        String day = param.get("day").toString();
        String templateId = param.get("templateId").toString();
        List<Map<String,Object>> dutyList=(List<Map<String,Object>>)param.get("dutyList");
        int result= 0;
        try {
            result = dutyService.updateDuty(dutyList,day,templateId);
        } catch (ParseException e) {
            return ResultUtil.error(-1,"时间转换失败，需要yyyy-MM-dd类型时间");
        }
        return ResultUtil.success(result);
    }

    /**
     * 个人排班 加入排班账号名称
     * @param param
     * @return
     */
    @PostMapping("/updateDutyAndUserName")
    public Result updateDutyAndUserName(@RequestBody Map<String,Object> param){
        String day = param.get("day").toString();
        String templateId = param.get("templateId").toString();
        List<Map<String,Object>> dutyList=(List<Map<String,Object>>)param.get("dutyList");
        int result= 0;
        try {
            result = dutyService.updateDutyAndUserName(dutyList,day,templateId);
        } catch (ParseException e) {
            return ResultUtil.error(-1,"时间转换失败，需要yyyy-MM-dd类型时间");
        }
        return ResultUtil.success(result);
    }

    @GetMapping("/queryFiveDuty")
    public Result queryFiveDuty(){
        List<Map<String,Object>> result=dutyService.queryFiveDuty();
        return ResultUtil.success(result);
    }
    @GetMapping("/queryMonthDuty")
    public Result queryMonthDuty(String month){
        User subject = (User) SecurityUtils.getSubject().getPrincipal();
        if(null==subject){
            return ResultUtil.error(-1000,"用户未登录");
        }
        List<Map<String,Object>> result = null;
        Set<String> list = roleMapper.queryRoleCodeByUserId(subject.getId());
        if(list.contains("admin")){
            result = dutyService.queryMonthDuty(month);
        }else{
            result = dutyService.queryMonthDutyAndUserName(month,subject.getUserName());
        }

        return ResultUtil.success(result);
    }

}
